package com.huiyeji.dynamic.core.builder;

import com.huiyeji.dynamic.core.provider.DynamicAnnoMethodProvider;
import com.huiyeji.dynamic.core.provider.DynamicMethodProvider;
import com.huiyeji.dynamic.core.DynamicObject;
import com.huiyeji.dynamic.core.DynamicObjectInfo;
import com.huiyeji.dynamic.core.annotations.*;

import java.util.Arrays;

public abstract class AbstractDynamicBuilder implements DynamicBuilder {

    @DynamicMethod("dynamic")
    @Override
    public DynamicObject build(Object object) {
        if (object == null){
            return null;
        }
        DynamicObjectInfo info = initialize(object);
        if (info == null){
            return null;
        }
        DynamicMethodProvider methodProvider = buildDynamicMethodProvider(info);
        return new DynamicObject(info,methodProvider);
    }

    public abstract DynamicObjectInfo initialize(Object object);

    public DynamicMethodProvider buildDynamicMethodProvider(DynamicObjectInfo info){
        return new DynamicAnnoMethodProvider(Arrays.asList(info.getObject().getClass().getMethods()));
    }
}
